package com.cn.web.servlet;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cn.domain.Product;

public class AddCarServlet extends BaseServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		Product product = ps.getProductById(Integer.parseInt(id));
		Map<Product,Integer> map = (Map<Product, Integer>) request.getSession().getAttribute("cart");
		if(map == null){
			map = new LinkedHashMap<Product,Integer>();
			request.getSession().setAttribute("cart", map);
		}
		Integer num = map.put(product, 1);
		if(num!=null){
			map.put(product, num+1);
		}
		response.sendRedirect(request.getContextPath()+"/cart.jsp");
	}

}
